package com.hnhegui.backend.platform.controller.order;

import com.hnhegui.backend.entity.PageResult;
import com.hnhegui.backend.entity.Result;
import com.hnhegui.backend.platform.service.order.RestOrderService;
import com.hnhegui.backend.platform.vo.order.request.CreateOrderRequestVO;
import com.hnhegui.backend.platform.vo.order.response.CreateOrderResponseVO;
import com.hnhegui.backend.platform.vo.order.response.OrderDocumentResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单Controller
 */
@RestController
@RequestMapping("/rest/api/orders")
public class RestOrderController {

    @Autowired
    private RestOrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping
    public Result<CreateOrderResponseVO> createOrder(@Validated @RequestBody CreateOrderRequestVO request) {
        CreateOrderResponseVO response = orderService.createOrder(request);
        return Result.success(response);
    }

    /**
     * 用户订单查询
     */
    @GetMapping("/my")
    public Result<PageResult<OrderDocumentResponseVO>> getMyOrders(
            @RequestParam String userId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize) {
        
        PageResult<OrderDocumentResponseVO> orderPage = orderService.getUserOrders(userId, status, startTime, endTime, page, pageSize);
        return Result.success(orderPage);
    }

    /**
     * 订单详情查询
     */
    @GetMapping("/{orderId}")
    public Result<OrderDocumentResponseVO> getOrderDetail(@PathVariable String orderId) {
        OrderDocumentResponseVO order = orderService.getOrderById(orderId);
        return Result.success(order);
    }

    /**
     * 管理端订单查询（支持多条件组合查询）
     * 
     * @param orderNo 订单号
     * @param userId 用户ID
     * @param productName 商品名称关键字
     * @param receiverPhone 收件人手机号
     * @param status 订单状态
     * @param paymentStatus 支付状态
     * @param startTime 创建时间-开始
     * @param endTime 创建时间-结束
     * @param page 页码（默认1）
     * @param pageSize 每页条数（默认20）
     * @return 订单分页结果
     */
    @GetMapping("/admin")
    public Result<PageResult<OrderDocumentResponseVO>> getAdminOrders(
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String receiverPhone,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String paymentStatus,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize) {
        
        PageResult<OrderDocumentResponseVO> orderPage = orderService.searchAdminOrders(
            orderNo, userId, productName, receiverPhone, 
            status, paymentStatus, startTime, endTime, 
            page, pageSize
        );
        
        return Result.success(orderPage);
    }
}
